﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BitworkStudios.Scraper;
using BitworkStudios.Scraper.Document;

namespace AllHomesScraper
{
    public class PropertyDefinition : HtmlDocumentDefinitionBase, IHtmlDocumentDefinition
    {
        public PropertyDefinition()
        { }

        public PropertyDefinition(string documentUrl)
        {
            Synchronise(Populate<PropertyDefinition>(documentUrl), this);
        }

        [ElementDefinition(Type = NodeType.Link, NodePath = @"//a[@id='header_logo']")]
        public string RootUrl { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//div[@id='breadCrumbs']/a[3]")]
        public string Region { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//div[@id='breadCrumbs']/a[4]")]
        public string District { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//div[@id='breadCrumbs']/a[5]")]
        public string Suburb { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//h1[@class='address']")]
        public string Address { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//h2[@class='price']")]
        public string Price { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//span[@class='loan_stampDuty']")]
        public string StampDuty { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//span[@class='loan_legalFee']")]
        public string LegalFees { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//span[@class='loan_loanAppFee']")]
        public string LoanApplicationFees { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//span[@class='loan_minDeposit']")]
        public string MinimumDeposit { get; set; }

        [ElementDefinition(Type = NodeType.InnerText, NodePath = @"//span[@class='loan_initialPayment']")]
        public string TotalInitialPayment { get; set; }

        [ElementDefinition(Type = NodeType.Link, NodePath = @"//div[@id='left_container']/div[@class='column'][1]/div/a[@href]")]
        public string PhotosUrl { get; set; }

        [ElementDefinition(Type = NodeType.InnerHtml, NodePath = @"//p[@class='feature_list']")]
        public string FeatureListHtml { get; set; }

        //Not sure list
        [ElementDefinition(Ignore = true)]
        public string Bathrooms
        {
            get
            {
                string featuresList = ConditionHtml(FeatureListHtml);
                return GetFeature("Bathrooms", featuresList);
            }
        }

        [ElementDefinition(Ignore = true)]
        public string Bedrooms
        {
            get
            {
                string featuresList = ConditionHtml(FeatureListHtml);
                return GetFeature("Bedrooms", featuresList);
            }
        }

        [ElementDefinition(Ignore = true)]
        public string Garages
        {
            get
            {
                string featuresList = ConditionHtml(FeatureListHtml);
                return GetFeature("Garaging", featuresList);
            }
        }

        [ElementDefinition(Ignore = true)]
        public string Ensuites
        {
            get
            {
                string featuresList = ConditionHtml(FeatureListHtml);
                return GetFeature("Ensuites", featuresList);
            }
        }

        [ElementDefinition(Ignore = true)]
        public string Carports
        {
            get
            {
                string featuresList = ConditionHtml(FeatureListHtml);
                return GetFeature("Carports", featuresList);
            }
        }

        private string GetFeature(string imgTitle, string featureHtml)
        {
            Regex regex = new Regex(string.Format("<img src=\"[^\"]*\" alt=\"{0}\" title=\"{0}\">", imgTitle), RegexOptions.Compiled);
            string regexText = regex.Replace(featureHtml, "<|>");
            regexText = regexText.Replace(" ", ""); 
            regexText = regexText.Replace("x", "");

            char[] text = regexText.ToCharArray();
            var num = new StringBuilder();
            int count = 0;
            foreach (char c in text)
            {
                if(c == '<')
                {
                    count++;
                    continue;
                }

                if (c == '|' && count == 1)
                {
                    count++;
                    continue;
                }

                if (c == '>' && count == 2)
                {
                    count++;
                    continue;
                }

                if (count ==3)
                {
                    try
                    {
                        var i = (int) c;
                        num.Append(c);
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    continue;
                }

                count = 0;
            }

            return num.ToString();
        }
    }
}
